﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcFinalTest.Data;
using MvcFinalTest.Models;
using System.Collections.ObjectModel;

namespace MvcFinalTest.Controllers
{
    public class JobTypeController : Controller
    {
        //
        // GET: /JobType/

        public ActionResult Index()
        {
            Collection<JobTypeModel> jobtype = JobTypeRepository.GetAll();
            ViewData["JobTypeData"] = jobtype.Count().ToString();
            return View(jobtype);
        }

        //
        // GET: /JobType/Details/5

        public ActionResult Details(int id)
        {
            JobTypeModel jobtype = JobTypeRepository.GetJobTypeById(id);
            return View(jobtype);
        }

        //
        // GET: /JobType/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /JobType/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            JobTypeModel jobtype = new JobTypeModel();
            try
            {
                // TODO: Add insert logic here
                if (TryUpdateModel(jobtype))
                {
                    if (ModelState.IsValid)
                    {
                        JobTypeRepository.Add(jobtype);
                        return RedirectToAction("Index");
                    }                    
                }               
            }
            catch
            {
                return View(jobtype);
            }
            return View(jobtype);
        }
        
        //
        // GET: /JobType/Edit/5
 
        public ActionResult Edit(int id)
        {
            JobTypeModel jobtype = JobTypeRepository.GetJobTypeById(id);
            return View(jobtype);
        }

        //
        // POST: /JobType/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            JobTypeModel jobtype = JobTypeRepository.GetJobTypeById(id);
            try
            {
                if (TryUpdateModel(jobtype))
                {
                    if (ModelState.IsValid)
                    {
                        JobTypeRepository.Edit(jobtype);
                        return RedirectToAction("Index");
                    }
                }
            }
            catch
            {
                return View(jobtype);
            }
            return View(jobtype);
        }

        //
        // GET: /JobType/Delete/5

        public ActionResult Delete(int id)
        {
            JobTypeRepository.Delete(id);
            return RedirectToAction("Index");
        }
    }
}
